package com.sniper.springmvc.action.jyzx;

import com.gentle.service.comply.TransdataService;
import com.gentle.service.declare.Exception_Exception;
import com.gentle.service.declare.IDataService;
import com.gentle.service.declare.JyzxBack;
import com.sniper.springmvc.action.RootController;
import com.sniper.springmvc.jyzx.JyzxData;
import com.sniper.springmvc.jyzx.JyzxUtils;
import com.sniper.springmvc.utils.PageUtil;
import com.sniper.springmvc.utils.ValidateUtil;
import org.apache.shiro.web.util.WebUtils;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * 数据共享方案

 * Created by suzhen on 2017/4/6.
 */
@Controller
public class JyzxController extends RootController {

    public static final String TYPE = "jyzxgonggao01";

    @RequestMapping("jyzx")
    public String index(Map<String, Object> map) throws Exception_Exception, NoSuchMethodException, IllegalAccessException, DocumentException, InvocationTargetException {

        //每页显示数据
        int pageSize = 50;
        //分页起始
        int pageOffset = 0;

        String pageNoStr = WebUtils.getCleanParam(request, "pageNo");
        if (!ValidateUtil.isValid(pageNoStr)) {
            pageNoStr = "1";
        }

        int pageNo = Integer.valueOf(pageNoStr).intValue();
        if (pageNo == 1) {
            pageOffset = 0;
        } else {
            pageOffset = (pageNo - 1) * pageSize;
        }

        TransdataService t = new TransdataService();
        IDataService iDataService = t.getDataServicePort();
        String dataStr = iDataService.getdata(TYPE, pageOffset, pageSize);

        JyzxData jyzxData = JyzxUtils.decodeStr(dataStr);
        int total = Integer.valueOf(jyzxData.getTotal());

        PageUtil page = new PageUtil(total, pageSize);
        page.setRequest(request);
        String pageHtml = page.show();
        List<JyzxBack> lists = jyzxData.getBacks();
        map.put("list", lists);
        map.put("pageHtml", pageHtml);
        return forward("/admin/admin-links/index.jsp");
    }
}
